import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author Stringzhua
 * @Date 2024/10/31 14:58
 * description:
 */
public class Demo03 {
    public static void main(String[] args) {
        //消息阻塞队列
        LinkedBlockingQueue<String> blockingQueue = new LinkedBlockingQueue<>(5);

        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(6, 6, 0,
                TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(5));

        //5个消费者,5个线程
        for (int i = 0; i < 5; i++) {
            poolExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            String data = blockingQueue.take();
                            System.out.println(Thread.currentThread().getName() + "线程消费并读取到[" + data + "]数据");
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }

        //1个生产者,1个线程
        poolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    for (int i = 0; i < 10000; i++) {
                        blockingQueue.put("Data[" + i + "]");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
